using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Package;
using System.Reflection;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio;
using NBusiness.CodeDom.Compiler;
using NBusiness.ESharp;
using NBusiness.CodeDom;
using NBusiness.VisualStudio.Engine;
using NBusiness.ESharp.Compiler;

namespace NBusiness.VisualStudio.Language
{
    internal class ESharpScope : AuthoringScope
    {
        ESharpLanguageService _language;
        ParseRequest _request;

        public ESharpScope(ESharpLanguageService language, ParseRequest request) 
        {
            _language = language;
            _request = request;
        }

        public override string GetDataTipText(int line, int col, out TextSpan span)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override Declarations GetDeclarations(IVsTextView view, int line, int col, TokenInfo info, ParseReason reason)
        {
            EntityRoot root = _language.GetCurrentRoot();
            ESharpDeclarations declarations = new ESharpDeclarations();

            lock (root)
            {
                try
                {
                    EntityElement element = root.GetElementByLocation(_request.FileName, line, col);

                    if (element != null)
                    {
                        info.StartIndex = element.BeginOffset;
                        info.EndIndex = element.EndOffset;
                        declarations.AddRange(ESharpDeclaration.GetDeclarations(root, element, reason));
                    }
                    declarations.Sort();
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex, "Scope");
                }
            }

            return declarations;
        }

        public override Methods GetMethods(int line, int col, string name)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override string Goto(VSConstants.VSStd97CmdID cmd, IVsTextView textView, int line, int col, out Microsoft.VisualStudio.TextManager.Interop.TextSpan span)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
